#include "user.h"
using namespace std;
vector<user> users;
tm createTm(int year, int month, int day, int hour, int minute, int second) {
    tm timeinfo;
    timeinfo.tm_year = year - 1900; // Years since 1900
    timeinfo.tm_mon = month - 1;    // Months since January [0-11]
    timeinfo.tm_mday = day;         // Day of the month [1-31]
    timeinfo.tm_hour = hour;        // Hours since midnight [0-23]
    timeinfo.tm_min = minute;       // Minutes after the hour [0-59]
    timeinfo.tm_sec = second;       // Seconds after the minute [0-60]
    timeinfo.tm_isdst = -1;         // Daylight saving time flag

    // Automatically calculate tm_wday and tm_yday
    mktime(&timeinfo);

    return timeinfo;
}
void addTask(const Task &task, user &User)
{
	// 在User用户中添加任务task
	User.usertask.push_back(task);
    ofstream outFile(User.filename);
	if (outFile)
	{
		task.saveToFile(outFile);
	}
    outFile.close();
}
void deleteTask(int id, user &User)
{
	bool flag = false;
    for (int i = 0; i < int(User.usertask.size()); i++)
	{
		if (User.usertask[i].id == id)
		{
			User.usertask.erase(User.usertask.begin() + i);
			flag = true;
		}
	}
	if (!flag)
	{
		cout << "failed,local stroage has no this id" << endl;
		return;
	}
    ofstream outFile(User.filename);
    for (int j = 0; j < int(User.usertask.size()); j++)
	{
        User.usertask[j].saveToFile(outFile);
	}
    outFile.close();
}
void deleteTask(string targetName, user &User)
{
	// 在User用户对应的文件中删除任务，并在User用户中的Task中删除
	bool flag = false;
    for (int i = 0; i < int(User.usertask.size()); i++)
	{
		if (User.usertask[i].name == targetName)
		{
			User.usertask.erase(User.usertask.begin() + i);
			flag = true;
		}
	}
	if (!flag)
	{
		cout << "failed,local stroage has no this name" << endl;
		return;
	}
    ofstream outFile(User.filename);
    for (int j = 0; j < int(User.usertask.size()); j++)
	{
        User.usertask[j].saveToFile(outFile);
	}
    outFile.close();
}

vector<user> loadUsers(){
    ifstream inFile("users.txt");
    if (!inFile.is_open()) {
        cerr << "Failed to open file: " << "users.txt" << endl;
        exit(-1);
    }

    vector<user> Users;
    string user_id, password, filename;

    while (inFile >> user_id >> password >> filename) {
        user User(user_id, password, filename);
        Users.push_back(User);
    }

    inFile.close();
    return Users;
}

// 从文件加载任务
static Task loadFromFile(ifstream& inFile) {
    Task task;
    int priority, type;
    string startTimeStr, remindTimeStr;
    inFile >> task.id >> task.name >> startTimeStr >> priority >> type >> remindTimeStr;
    task.startTime = task.stringToTime(startTimeStr);
    task.priorityLevel = static_cast<Priority>(priority);
    task.category = static_cast<Type>(type);
    task.remindTime = task.stringToTime(remindTimeStr);
    return task;
}

void loadTasks(user &user) {
    ifstream inFile(user.filename);
    if (!inFile.is_open()) {
        cerr << "Failed to open file: " << user.filename << endl;
        return;
    }

    while (true) {
        Task task = loadFromFile(inFile);
        if (inFile.eof()) break;
        user.usertask.push_back(task);
    }

    inFile.close();
}

void show_vectortask(vector<Task> tasks)
{
	// 基本输出任务，输出vector Task
	// 还没写好
}

void showPrioritytasks(user &User)
{
	vector<Task> htask, mtask, ltask;
    for (int i = 0; i < int(User.usertask.size()); i++)
	{
		switch (User.usertask[i].priorityLevel)
		{
		case 0:
			htask.push_back(User.usertask[i]);
			break;
		case 1:
			mtask.push_back(User.usertask[i]);
			break;
		case 2:
			ltask.push_back(User.usertask[i]);
			break;
		}
	}
	show_vectortask(htask);
	show_vectortask(mtask);
	show_vectortask(ltask);
}
void showDaytasks(user &User, int year, int month, int day)
{
	// 按天输出
	vector<Task> daytasks;
    for (int i = 0; i < int(User.usertask.size()); i++)
	{
		if (User.usertask[i].startTime.tm_year == year &&
			User.usertask[i].startTime.tm_mday == day &&
			User.usertask[i].startTime.tm_mon == month)
			daytasks.push_back(User.usertask[i]);
	}
	show_vectortask(daytasks);
}
void showMonthtasks(user &User, int year, int month)
{
	// 按月输出
	vector<Task> monthtasks;
    for (int i = 0; i < int(User.usertask.size()); i++)
	{
		if (User.usertask[i].startTime.tm_year == year &&
			User.usertask[i].startTime.tm_mon == month)
			monthtasks.push_back(User.usertask[i]);
	}
	show_vectortask(monthtasks);
}
void get_t(int &year, int &month, int &day)
{
	// 两个重载，分别为日和月的时间
	cout << "input year,month and day" << endl;
	cin >> year >> month >> day;
}
void get_t(int &year, int &month)
{
	cout << "input year,month" << endl;
	cin >> year >> month;
}
void showTasks(user &User, showType type)
{
	switch (type)
	{
	case PRIORITY:
		showPrioritytasks(User);
		break;
	case DAY:
	{
		int year, month, day;
		get_t(year, month, day);
		showDaytasks(User, year, month, day);
		break;
	}
	case MONTH:
	{
		int year, month;
		get_t(year, month);
		showMonthtasks(User, year, month);
		break;
	}
	}
}
int find_user(string id)
{
	// 在所有用户中查找该id
    for (int i = 0; i < int(users.size()); i++)
	{
		if (users[i].user_id == id)
			return i;
	}
	return -1;
}
void get_user_message()
{
	cout << "please input your id" << endl;
	int index = -1;
	string id, code;
	cin >> id;
	if ((index = find_user(id)) != -1)
	{
		cout << "please input your code" << endl;
		cin >> code;
		if (users[index].code == user::get_hashcode(code))
		{
			cout << users[index].filename << "are opened successfully" << endl;

			return;
		}
		cout << "error,your code is wrong!" << endl;
	}

	// 未完待续
}
